package com.molyfun.parents.activity.store;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.store.adapter.ChooseChildrenListAdapter;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.OrganizationListResponse;
import com.molyfun.parents.rest.bean.ProductDetailResponse;
import com.molyfun.parents.utils.Const;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.molyfun.parents.utils.Const.PRODUCT_ID;

/**
 * Created by Administrator on 2017/12/1 0001.
 */

public class ChooseChildrenInfoActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.recyclerView_choose_children)
    RecyclerView mRecyclerView;
    @InjectView(R.id.base_title_left)
    ImageView base_title_left;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.submit)
    TextView mSubmit;

    private String productId = "";
    private String mDepartName = "";
    private LinearLayoutManager mLayoutManager;
    private ChooseChildrenListAdapter mListAdapter;
    private List<OrganizationListResponse.Organization> mDepartList = new ArrayList<>();

    private ArrayList<ProductDetailResponse.ProductDetailBean> mDataList;

    public static void startActivity(Context context, ProductDetailResponse.ProductDetailBean productDetailBean) {
        ArrayList<ProductDetailResponse.ProductDetailBean> list = new ArrayList<>();
        list.add(productDetailBean);
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(Const.PRODUCT_DETAIL_BEAN_LIST, list);
        intent.setClass(context, ChooseChildrenInfoActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, ProductDetailResponse.ProductDetailBean productDetailBean, String departName) {
        ArrayList<ProductDetailResponse.ProductDetailBean> list = new ArrayList<>();
        list.add(productDetailBean);
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(Const.PRODUCT_DETAIL_BEAN_LIST, list);
        intent.putExtra(Const.DEPART_NAME,departName);
        intent.setClass(context, ChooseChildrenInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_children);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");

        productId = getIntent().getStringExtra(PRODUCT_ID);
        mDepartName = getIntent().getStringExtra(Const.DEPART_NAME);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mListAdapter = new ChooseChildrenListAdapter(this);
        mRecyclerView.setAdapter(mListAdapter);

        base_title_left.setOnClickListener(this);
        mSubmit.setOnClickListener(this);

        mDataList = getIntent().getParcelableArrayListExtra(Const.PRODUCT_DETAIL_BEAN_LIST);

        getOrganizationList();
    }

    private void getOrganizationList() {
        showProgressDialog();
        Call<OrganizationListResponse> call = MFPApplication.getInstance().getRestClient().getOrganizationList("pass", RestClient.DEFAULT_PAGE, RestClient.LARGE_PAGE_SIZE);
        call.enqueue(new Callback<OrganizationListResponse>() {
            @Override
            public void onResponse(Call<OrganizationListResponse> call, Response<OrganizationListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null && response.body().getData() != null) {
                    if (response.body().getData().size() > 0) {
                        List<OrganizationListResponse.Organization> organizationList = response.body().getData();
                        for (OrganizationListResponse.Organization organization : organizationList) {
//                            if (PreferencesStorageUtil.getDepartname(ChooseChildrenInfoActivity.this).equals(organization.getDepartname())) {
                            if (!TextUtils.isEmpty(mDepartName) && mDepartName.equals(organization.getDepartname())) {
                                mDepartList.add(organization);
                                mListAdapter.refreshData(mDepartList,mDepartName);
                                mEmptyLayout.setVisibility(View.GONE);
                            }
                        }
                    } else {
                        mListAdapter.clearAll();
                        mEmptyLayout.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onFailure(Call<OrganizationListResponse> call, Throwable t) {
                mListAdapter.clearAll();
                mEmptyLayout.setVisibility(View.VISIBLE);
                dismissProgressDialog();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.submit:
                if (mDataList != null && mDataList.size() > 0 && mListAdapter.getChoosedOrganization() != null) {
//                    OrderDetailActivity.startActivity(this, mDataList, mListAdapter.getChoosedOrganization().getStudentid());
                    finish();
                }
                break;
        }
    }
}
